// apis/uni-fetch.js

// 导入安装好的 uni-app-fetch 模块
import {
	createUniFetch
} from 'uni-app-fetch'
import store from '@/store'
import {
	getToken,
	removeToken,
	removeUserInfo
} from '@/utils/auth'

// 定义 tabBar 的路径
const tabBarPagePaths = [
	'pages/index/index',
	// 'pages/message/index',
	'pages/my/index'
]
// 图片前缀
const imgBaseUrl = 'https://oss.yukukeji.com/';

const baseURLList = ['http://192.168.3.37:8808/yuku/api/miniApp', 'http://192.168.3.18:8808/yuku/api/miniApp',
	'https://test.yukukeji.com/yuku/api/miniApp', 'https://www.yukukeji.com/yuku/api/miniApp'
]
// 0进龙 1鑫雨 2测试 3正式
const baseURL = baseURLList[2];

// const imgBaseUrl = 'https://www.yukukeji.com/static'
// 配置符合自身业务的请求对象
// let num = 0;
export const uniFetch = createUniFetch({
	loading: {
		title: '正在加载...',
		mask: true
	},
	baseURL,
	intercept: {
		// 请求拦截器
		request(options) {
			// num++;
			options.timeout = 5000;
			if (store.getters.token) {
				const defaultHeaders = {
					token: getToken(),
				}
				options.header = Object.assign({}, defaultHeaders, options.header, )
			}
			return options
		},
		// 响应拦截器
		response(res) {
			let {
				data
			} = res;
			console.log(res, "res");
			// num--;
			if (data.code === 401) {
				// if (num <= 0) {
					// num = 0;
				removeToken()
				removeUserInfo()
				// 页面栈
				const pageStack = getCurrentPages()
				console.log(pageStack, 'pageStack');
				let query = location ? location.search.substring(1) : ''
				console.log(query, 'query')
				const storeId = query ? query.split('=')[1] : ''
				// 当前页面地址
				let redirectURL = pageStack[pageStack.length - 1]?.route ? pageStack[pageStack.length - 1].route : ''
				// redirectURL = 'pages/index/index'; 
				console.log('redirectURL', redirectURL)
				// tabBar 页面需要使用 switchTab 跳转
				const routeType = tabBarPagePaths.includes(redirectURL) ?
					'switchTab' :
					'redirectTo'

				return uni.redirectTo({
					url: `/pages/login/index?storeId=${storeId}&routeType=${routeType}&redirectURL=/${redirectURL}`,
				})
				// }
			}
			return data
		},
	},
})


export const fileUpload = filePath => {
	uni.showLoading({
		title: "图片处理中",
		mask: true
	})
	const lastIndex = filePath.lastIndexOf('.');
	let filetype = '';
	if (lastIndex !== -1) {
		filetype = filePath.substring(lastIndex + 1);
	}
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: baseURL + '/file/uploadFile',
			filePath,
			header: {
				'Content-Type': 'multipart/form-data',
				token: getToken(),
			},
			formData: {
				// 该字段是后端除了需要文件之外的其他参数
				fileType: 'image/' + filetype
			},
			// name的值为后端接口与文件相对于的字段名
			name: 'multipartFile',
			success(res) {
				if (res.statusCode == 200) {
					const fileObj = JSON.parse(res.data).data;
					let onlineFilePath = imgBaseUrl + fileObj.filePath + fileObj.fileNewName;
					uni.hideLoading();
					resolve(onlineFilePath);
				} else {
					uni.hideLoading();
					uni.utils.toast("图片上传失败");
					reject(res)
				}
			},
			fail(f) {
				console.log('f', f);
				uni.hideLoading();
				reject(f)
			},
		})
	})
}